// pages/personal/CoachIn/index.js
var config = require('../../../utils/config')
const app = getApp();
const ajax = app.myRequest()
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    image:[],         // 教练照片
    name:'',          // 姓名
    phone:'',         // 电话
    type:'',          // 擅长类型
    address:'',       // 地址
    introduce:'',     // 介绍
    Portrait:[],
    img:[],
    checked:false,
    errorName:false,
    errorPhone:false,
    errorGender:false,
    errorType:false,
    errorIntroduce:false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if(options.scene){
      var gymnasium_id = options.scene
      this.setData({gymnasium_id})
    }

    
  },


  // 姓名
  getName(e){
    this.setData({
      name:e.detail.value,
      errorName:false
    })
  },
  Gender(e){
    this.setData({
      Gender:e.detail.value
    })
    if(e.detail.value = '男'){
      this.setData({sex:1,errorGender:false})
    }else if(e.detail.value = '女'){
      this.setData({sex:2,errorGender:false})
    } else{
      this.setData({sex:3})
    }
    

  },
  // 电话
  getTelephone(e){
    console.log(e)
    var telephone = e.detail.value
    if(telephone.length==0){
      this.setData({
        fals : true,
        telephoneText : '电话号不能为空'  
      })
    } else if(!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(telephone))){
      this.setData({
        fals : true,
        telephoneText : '请输入正确电话号'  
      })
    } else{
      this.setData({
        fals : false
      })
    }
    this.setData({
      phone : telephone,
      errorPhone:false
    })
  },
  // 教练擅长类型
  getType(e){
    this.setData({
      type:e.detail.value,
      errorType:false
    })
  },

  // 介绍
  getIntroduce(e){
    this.setData({
      introduce:e.detail.value,
      errorIntroduce:false
    })
  },

  // 地址

  // 身份证正面
  afterRead(event){
    const file = event.detail.file.path;
    var _this = this
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: config.domain+'/File/upload', 
      filePath: file,
      name: 'file',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      formData: { 'token': _this.data.token },
      success(res) { 
        console.log(res)       
        console.log({ ...file, url: res.data})
        var file = { ...file, url:JSON.parse(res.data)}
        var positive = file.url.data.url
        var img = config.picture+file.url.data.url
        var fileList = []
        fileList.push({url:img})
        console.log(fileList)
        _this.setData({
          fileList,
          positive
        })
      },
    });
  },

  
  // 删除图片第二张
  deletePictureOne(){
    console.log('111')
    this.setData({
      picture:[]
    })
  },


  // 删除图片第一张
  deletePicture(){
    this.setData({
      fileList:[]
    })
  },

  // 上传图片第二张
  getPicture(event){
    console.log(this.data.token)
    const file = event.detail.file.path;
    var _this = this
    wx.uploadFile({
      url: config.domain+'/File/upload',
      filePath: file,
      name: 'file',
      header: {
        "Content-Type": "multipart/form-data"
      },
      formData: { 'token': _this.data.token },
      success(res) {
        console.log(res)     
        console.log({ ...file, url: res.data.data})
        var file = { ...file, url:JSON.parse(res.data)}
        console.log(config.picture+file.url.data.url)
        var img = config.picture+file.url.data.url
        var theOtherSide = file.url.data.url
        var picture = []
        picture.push({url:img})
        console.log(picture)
        _this.setData({
          picture,
          theOtherSide
        })
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  
  // 教练的肖像
  setPortrait(event){
    const { file } = event.detail;
    var _this = this
    wx.uploadFile({
      url: config.domain+'/File/upload',
      filePath: file.path,
      name: 'file',
      formData: { 'token': _this.data.token },
      success(res) {
        console.log(res.data)
        var resData = JSON.parse(res.data)
        // console.log(resData.data.url)
        // 上传完成需要更新 fileList
        const { Portrait = [] } = _this.data;
        Portrait.push({ ...file, url: config.picture + resData.data.url });
        var getPortrait = resData.data.url
        console.log(Portrait)
        _this.setData({ Portrait , getPortrait});
      },
    });
  },

  toAgreement(){
    wx.navigateTo({
      url: '/pages/personal/CoachAgreement/index',
    })
  },

  // 上传教练 照片 
  // setImage(event){
  //   var img = this.data.img
  //   const { file } = event.detail;
  //   var _this = this
  //   wx.uploadFile({
  //     url: config.domain+'/File/upload',
  //     filePath: file.path,
  //     name: 'file',
  //     formData: { 'token': _this.data.token },
  //     success(res) {
  //       // console.log(res.data)
  //       var resData = JSON.parse(res.data)
  //       console.log(resData.data.url)
  //       // 上传完成需要更新 fileList
  //       const { image = [] } = _this.data;

  //       image.push({ ...file, url: 'https://yue.7758521.work' + resData.data.url });
  //       img.push(resData.data.url)
  //       console.log(img)
  //       _this.setData({ image ,img});
  //     },
  //   });
  // },

  // 删除教练图片
  // deleteImage(event){
  //   console.log(event)
  //   var id = event.detail.index
  //   var image = this.data.image
  //   image.splice(id,1);
  //   this.setData({ image })
  // },

  

  // 同意协议
  onChange(event){
    console.log(event)
    
    this.setData({
      checked : event.detail
    })
  },

  /**
   * 生命周期函数--监听页面显示
   * navigateBack
   */
  onShow: function () {
    var _this = this
    wx.getSetting({
      success:function(res){
        if(res.authSetting['scope.userInfo']){
          var token = wx.getStorageSync('token')
          _this.setData({ token })
          ajax.getRequest('/User/profile').then((res)=>{
            console.log(res.data.data.coach)
            if(res.data.data.coach){
              Dialog.alert({
                message: '您已经入驻为教练了',
              }).then(()=>{
                wx.switchTab({
                  url: '/pages/index/index',
                })
              })
            }
          })
        } else{
          wx.navigateTo({
            url: '/pages/login/SignIn/index',
          })
        }
      }
    })
  },
  navigateBack(){
    wx.switchTab({
      url: '/pages/personal/my/index',
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  // 提交申请
  getSubmit(){
    var gymnasium_id = this.data.gymnasium_id
    var name = this.data.name
    var phone = this.data.phone
    var sex  = this.data.sex
    var speciality = this.data.type
    var portrait_image = this.data.getPortrait
    var content = this.data.introduce
    // var card_up_image = this.data.positive
    // var card_down_image = this.data.theOtherSide
    console.log(name)
    console.log('提交')
    // if(gymnasium_id && name !='' && phone !='' && sex !='' && speciality !='' && content !='' && card_up_image !='' && card_down_image !='' && portrait_image != ''){
    //   ajax.postRequest('/Coach/create',{gymnasium_id,name,phone,sex,speciality,content,card_up_image,card_down_image}).then((res)=>{
    //     wx.showModal({
    //       title: '提示',
    //       content: '已提交入驻，请等待审核',
    //       success: function (res) {
    //         if (res.confirm) {
    //           wx.switchTab({
    //             url: '/pages/index/index',
    //           })
    //         } else{
              
    //         }
    //       }
    //     })
    //   })
    // } else{
    //   wx.showToast({
    //     title: '请填写完整信息',
    //     mask: true,
    //     icon: 'none',
    //     duration: 2000
    //   })
    // }


    console.log(gymnasium_id)

    if(this.data.checked){
      if(name==""){
        Dialog.alert({
          message: '请填写姓名',
        });
        this.setData({errorName:true})
      } else if(phone==''){
        Dialog.alert({
          message: '手机号不能为空',
        });
        this.setData({errorPhone:true})
      } else if(speciality==''){
        Dialog.alert({
          message: '擅长类型不能为空',
        });
        this.setData({errorType:true})
      } else if(sex==''){
        Dialog.alert({
          message: '性别不能为空',
        });
        this.setData({errorGender:true})
      } else if(content==''){
        Dialog.alert({
          message: '介绍不能为空',
        });
        this.setData({errorIntroduce:true})
      } else if(this.data.Portrait==''){
        Dialog.alert({
          message: '肖像照不能为空',
        });
      } 
      // else if(this.data.fileList==''){
      //   Dialog.alert({
      //     message: '身份证正面不能为空',
      //   });
      // } else if(this.data.picture==''){
      //   Dialog.alert({
      //     message: '身份证反面不能为空',
      //   });
      // }
      else{
        // ajax.postRequest('/Coach/create',{gymnasium_id,name,phone,sex,speciality,content,card_up_image,card_down_image,portrait_image}).then((res)=>{
        ajax.postRequest('/Coach/create',{gymnasium_id,name,phone,sex,speciality,content,portrait_image}).then((res)=>{
          console.log(res)
          if(res.data.msg=="提交成功，请等待场馆审核"){
            Dialog.alert({
              message: '提交成功，请等待场馆审核',
            }).then(()=>{
              wx.switchTab({
                url: '/pages/personal/my/index',
              })
            })
          } else{
            Dialog.alert({
              message: res.data.msg,
            })
          }
        })
      }
    } else{
      Dialog.alert({
        message: '请同意协议',
      });
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})